package top.tupobi.zing.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;

import java.util.Timer;
import java.util.TimerTask;

import top.tupobi.zing.network.HttpConfig;
import top.tupobi.zing.service.socket.ClientSocketProcessor;
import top.tupobi.zing.service.socket.ProcessPacket;
import top.tupobi.zing.service.socket.listener.OnSocketListener;
import top.tupobi.zing.service.socket.listener.SocketResponseListener;
import top.tupobi.zing.service.socket.packet.SocketPacket;
import top.tupobi.zing.utils.CPUThreadPool;

public class IMService extends Service {

    private ClientSocketProcessor mClientSocketProcessor = new ClientSocketProcessor();
    private ProcessPacket mProcessPacket = new ProcessPacket(mClientSocketProcessor);

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("IMService onBind");
        return new IMBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("IMService onCreate");
        LogUtils.e("IMService onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("IMService onStartCommand");
        LogUtils.e("IMService onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("IMService onUnbind");
        LogUtils.e("IMService onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        System.out.println("IMService onDestroy");
        LogUtils.e("IMService onDestroy");
        super.onDestroy();
    }

    //创建服务代理，调用服务中的方法
    public class IMBinder extends Binder {

        public void setSocketResponseListener(SocketResponseListener socketResponseListener) {
            mProcessPacket.setSocketResponseListener(socketResponseListener);
        }

        public void connectSocketAndPostHeartbeat() {
            System.out.println("IMService connectSocketAndPostHeartbeat");
            mClientSocketProcessor.setOnSocketListener(new OnSocketListener() {
                @Override
                public void onUnknownHostError() {
                    System.out.println("onUnknownHostError");
                    LogUtils.e("onUnknownHostError");
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            connectSocketAndPostHeartbeat();
                        }
                    }, HttpConfig.SOCKET_TIME_OUT);
                }

                @Override
                public void onConnectFail() {
                    System.out.println("onConnectFail");
                    LogUtils.e("onConnectFail");
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            connectSocketAndPostHeartbeat();
                        }
                    }, HttpConfig.SOCKET_TIME_OUT);
                }

                @Override
                public void onReceiveData(String data) {
                    processPacket(data);
                }
            });

            if (!mClientSocketProcessor.isSocketConnected()) {
                // 连接前判空，socket服务是否被销毁，且socket线程已经脱离service了，service已经被销毁，socket还在连接
                CPUThreadPool.getInstance().execute(() -> {
                    mClientSocketProcessor.connect(HttpConfig.SOCKET_IP, HttpConfig.SOCKET_PORT);
                    mProcessPacket.postHeartbeat();
                });
            }

        }

        public void addFriend(SocketPacket packet) {
            mProcessPacket.postPacket(packet);
        }

        public void sendMessage(SocketPacket packet) {
            mProcessPacket.postPacket(packet);
        }
    }

    private void processPacket(String data) {
        System.out.println("接收服务端的str == " + data);
        mProcessPacket.parsePacket(data);
    }
}
